// Car/ Car View

(function( window, undefined ) {

var GameStatus = Class.Model.GameInfo.GameStatus;

var CarColor = {
	Red: "red",
	Blue: "blue",
	Yellow: "yellow"
};

var Car = Class({
	name: "Car",
	parent: "View",
	constructor: function() {
		var _info = null;
		var _$object = $("<div class='car'></div>").html(
			"<div class='tier' ></div>"+
			"<div class='color' ></div>"+
			"<div class='driver' ></div>"
		);
		
		var _$tier = _$object.find(".tier");
		var _$color = _$object.find(".color");
		var _$driver = _$object.find(".driver");
		
		return {
			__construct: function( info ) {
				console.log( this, "__construct" );
				
				_info = info;
			},
			
			object:function() {
				return _$object;
			},
			
			bounceDriver: function() {
				_$driver.stop();
				
			},
			
			play: function( callback ) {
				console.log( this, "play" );
				
				_$object.stop().animate( {
					left:"150"
				}, 500, function() {
					callback();
				});
			},
			
			stop: function( callback ) {
				console.log( this, "stop" );
				
				_$object.stop().animate({
					bottom:"20"
				}, 150, "easeInQuad", function() {
					_$object.animate( {
						left:"90", bottom:"20"
					}, 100 * _info.speed(), "easeOutQuad", function() {
						_$object.animate( {
							left:"100"
						}, 200, "linear", function() {
							callback();
						});
					});
				});
			},
			
			jump: function( callback ) {
				console.log( this, "jump" );
				
				_$object.stop().animate( {
					left:"130",
					bottom:"140"
				}, 350, "easeOutQuad", function() {
					_$object.animate( {
						left:"110",
						bottom:"20"
					}, 350, "easeInQuad", function() {
						_$object.animate( {
							left:"150"
						}, 350, "swing", function() {
							callback();
						});
					});
				});
			},
		
			update: function() {
				console.log( this, "update" );
				
				if ( _info.status() >= GameStatus.Stopping ) {
					_$tier.addClass( "playing" );
				}
				else {
					_$tier.removeClass( "playing" );
				};
				
				_$color.removeClass( CarColor.Red );
				_$color.removeClass( CarColor.Blue );
				_$color.removeClass( CarColor.Yellow );
				
				_$color.addClass( _info.color() );
			}
		};
	}
});

Car.CarColor = CarColor;

Class.View.Car = Car;

})(window);